<html>
<head>
    <title>甘肃全国移动ca互认交易平台测试</title>
    <meta charset="UTF-8"/>
    <script src="https://anquan.baidu.com/webstatic/lib/jquery.min.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .layoutFlex{
            display: flex;
            margin-bottom: 20px;
            gap: 20px;
        }
    </style>
    <script>
        // 登录二维码
        function generateQRCode() {
            $.ajax({
                url: '/nation/getLoginQrCode',
                dataType: 'json',
                type: 'post',
                success: function(data) {
                    console.log(data);
                    if(data.code==0){
                        $('#loginTid').val(data.data.tid);

                        $('#loginQrCode').attr('src', 'data:image/png;base64,'+data.data.qrCodeImageBase64);
                        $('#loginQrCode').show();
                        intervalId = setInterval(function(){
                            querLoginStatus(data.data.tid,intervalId)
                        }, 1000)
                    }
                },
                error: function(xhr, status, error) {
                    console.log(error);
                }
            });
        }
        //登录二维码扫描状态
        function querLoginStatus(tid,intervalId) {
            $.ajax({
                url: '/nation/queryScannedStatus?tid='+tid,
                dataType: 'json',
                type: 'get',
                success: function(data) {
                    console.log(data);
                    if(data.code==0){
                        if(data.data.scannedStatus=='00'){
                            $('#loginStatus').text('未扫描');
                        }else if(data.data.scannedStatus=='01'){
                            $('#loginStatus').text('已扫描未授权');
                        }else if(data.data.scannedStatus=='02'){
                            clearInterval(intervalId);
                            $('#loginStatus').text('已失效');

                        }else if(data.data.scannedStatus=='03'){
                            clearInterval(intervalId);
                            getAuthUser(tid);
                            $('#loginStatus').text('已扫描授权');
                        }else{
                            clearInterval(intervalId);
                            $('#loginStatus').text('未知状态');
                        }

                    }
                    console.log(data);
                },
                error: function(xhr, status, error) {
                    console.log(error);
                }
            });
        }
        //获取授权用户
        function getAuthUser(tid){
            $.ajax({
                url: '/nation/getUserInfo?tid='+tid,
                dataType: 'json',
                type: 'get',
                success: function(data) {
                    console.log('获取用户授权信息:')
                    console.log(data);
                    if(data.code==0){
                        $('#authUser').val(JSON.stringify(data.data))
                        //主体共享平台信息拉取
                        var loginUserType=data.data.loginUserType
                        var threeInOneCode=loginUserType==="02"?data.data.orgCode:data.data.idCardHash
                        gsBodyLogin(threeInOneCode)
                    }
                },
                error: function(xhr, status, error) {
                    console.log(error);
                }
            });
        }
        //主体共享平台信息拉取
        function gsBodyLogin(threeInOneCode){
            $.ajax({
                url: '/nation/gsBodyLogin?threeInOneCode='+threeInOneCode,
                dataType: 'json',
                type: 'get',
                success: function(data) {
                    console.log('甘肃省主体共享平台信息:')
                    console.log(data)
                    if(data.mark==1){
                        alert('获取甘肃省主体共享平台信息成功！')
                    }else{
                        alert('获取甘肃省主体共享平台信息失败,请先注册')
                    }
                },
                error: function(xhr, status, error) {
                    console.log(error);
                }
            });
        }

        // 加密二维码
        function generateEnQRCode() {
            $.ajax({
                url: '/nation/getEnQrcode',
                dataType: 'json',
                type: 'post',
                success: function(data) {
                    console.log(data);
                    if(data.code==0){
                        $('#encryptTid').val(data.data.tid);
                        $('#enQrCode').attr('src', 'data:image/png;base64,'+data.data.qrCodeImageBase64);
                        $('#enQrCode').show();
                        // 轮询加密二维码扫描状态
                        intervalId = setInterval(function(){
                            queryEnScannedStatus(data.data.tid,intervalId)
                        }, 1000)
                    }
                },
                error: function(xhr, status, error) {
                    console.log(error);
                }
            });
        }
        //加密二维码扫描状态
        function queryEnScannedStatus(tid,intervalId) {
            $.ajax({
                url: '/nation/queryEnScannedStatus?tid='+tid,
                dataType: 'json',
                type: 'get',
                success: function(data) {
                    console.log(data);
                    if(data.code==0){
                        if(data.data.scannedStatus=='00'){
                            $('#loginStatus').text('未扫描');
                        }else if(data.data.scannedStatus=='01'){
                            $('#loginStatus').text('已扫描未授权');
                        }else if(data.data.scannedStatus=='02'){
                            clearInterval(intervalId);
                            $('#loginStatus').text('已失效');
                        }else if(data.data.scannedStatus=='03'){
                            clearInterval(intervalId);
                            $('#loginStatus').text('已扫描授权');
                            // 加密字符串
                            encryptString(data.data.tid, $('#waitEnMsg').val())
                        }else{
                            clearInterval(intervalId);
                            $('#loginStatus').text('未知状态');
                        }
                    }

                },
                error: function(xhr, status, error) {
                    console.log(error);
                }
            });
        }
        // 加密字符串
        function encryptString(tid,msg) {
            $.ajax({
                url: '/nation/encryptMsg?tid='+tid+'&msg='+msg,
                dataType: 'json',
                type: 'get',
                success: function(data) {
                    console.log('加密结果:')
                    console.log(data);
                    if(data.code==0){
                        $('#enMsg').val(JSON.stringify(data.data))
                    }
                },
                error: function(xhr, status, error) {
                    console.log(error);
                }
            });
        }

        // 解密二维码 几个步骤：
        // 1、生成解密二维码
        // 2、用户扫码后，获取用户授权信息、上传信封
        // 3、轮询解密数据
        function generateDeQRCode() {
            // var user=$('#authUser').val()
            // if(user==undefined||user==''){
            //     alert('请先获取用户授权信息');
            //     return
            // }
            var enMsg=$('#enMsg').val()
            if(enMsg==undefined||enMsg==''){
                alert('请输入加密数据')
                return;
            }
            //user=JSON.parse(user)
            enMsg=JSON.parse(enMsg)
            var decyptUserType=enMsg.certInfo.encryptionUserType
            var threeInOneCode=enMsg.certInfo.orgCode
            var idCardHash=enMsg.certInfo.idCardHash
            $.ajax({
                url: '/nation/getDeQrCode?decyptUserType='+decyptUserType+'&threeInOneCode='+threeInOneCode+'&idCardHash='+idCardHash,
                dataType: 'json',
                type: 'get',
                success: function(data) {
                    console.log(data);
                    if(data.code==0){
                        $('#decryptTid').val(data.data.tid);
                        $('#deQrCode').attr('src', 'data:image/png;base64,'+data.data.qrCodeImageBase64);
                        $('#deQrCode').show();

                        intervalId = setInterval(function(){
                            queryDeScannedStatus(data.data.tid,intervalId)
                        }, 1000)
                    }
                },
                error: function(xhr, status, error) {
                    console.log(error);
                }
            });
        }
        //获取解密用户信息
        function getDecryptToken(tid){
            $.ajax({
                url: '/nation/getDecryptUserInfo?tid='+tid,
                dataType: 'json',
                type: 'get',
                async: false,
                success: function(data) {
                    console.log('获取解密用户信息:')
                    console.log(data);
                    if(data.code==0){
                        token=data.data.accessToken
                        //上传信封信息
                        uploadEnvelope(tid,token)
                    }
                },
                error: function(xhr, status, error) {
                    console.log(error);
                }
            });
        }
        //上传信封信息
        function uploadEnvelope(tid,token){
           // var user=$('#authUser').val()
            var enMsg=$('#enMsg').val()
           // user=JSON.parse(user)
            enMsg=JSON.parse(enMsg)
            encryptedKeyDataEnvelopeList=[]
            encryptedKeyDataEnvelopeList.push({
                'encryptedKeyDataEnvelopeId':enMsg.encryptedKeyDataEnvelopeId,
                'caOrgCode':enMsg.certInfo.caOrgCode,
                'encryptionCertSn':enMsg.encryptionCertSn,
                'encryptionKey':enMsg.encryptionKey,
                'publicKeyAlgorithm':enMsg.publicKeyAlgorithm
            })
            var envelope={
                'tid':tid,
                'accessToken':token,
                'encryptedKeyDataEnvelopeList':encryptedKeyDataEnvelopeList
            }
            $.ajax({
                url: '/nation/uploadEncryptEnvelope',
                dataType: 'json',
                data:JSON.stringify(envelope),
                type: 'post',
                contentType: 'application/json;charset=UTF-8',
                success: function(data) {
                    console.log('上传信封信息:')
                    console.log(data);
                    intervalId=setInterval(function (){
                        getDecryptMsg(tid,intervalId)
                    },1000)
                },
                error: function(xhr, status, error) {
                    console.log(error);
                }
            });
        }
        //获取解密信息
        function getDecryptMsg(tid,intervalId) {
            $.ajax({
                url: '/nation/getDecryptMsg?tid='+tid,
                dataType: 'json',
                type: 'get',
                success: function(data) {
                    console.log('解密结果:')
                    console.log(data);
                    if(data.code==0&&data.data[0].decryptStatus=='0'){
                        clearInterval(intervalId);
                        $('#deMsg').val(JSON.stringify(data.data))
                    }
                },
                error: function(xhr, status, error) {
                    console.log(error);
                }
            });
        }
        //获取解密二维码扫描状态
        function queryDeScannedStatus(tid,intervalId) {
            $.ajax({
                url: '/nation/queryDeScannedStatus?tid='+tid,
                dataType: 'json',
                type: 'get',
                success: function(data) {
                    console.log(data);
                    if(data.code==0){
                        if(data.data.scannedStatus=='00'){
                            $('#loginStatus').text('未扫描');
                        }else if(data.data.scannedStatus=='03'){
                            //扫码之后状态就直接03了,获取用户token，上传信封信息
                            clearInterval(intervalId);
                            getDecryptToken(data.data.tid)
                        }else if(data.data.scannedStatus=='01'){
                            console.log('已扫码未授权 ')
                        }else if(data.data.scannedStatus=='02'){
                            clearInterval(intervalId);
                            console.log('已失效')
                        } else{
                            clearInterval(intervalId);
                            $('#loginStatus').text('未知状态');
                        }
                    }

                },
                error: function(xhr, status, error) {
                    console.log(error);
                }
            });
        }

        //签名二维码
        function generateSignQRCode() {
            var msg=$('#signMsg').val()
            $.ajax({
                url: '/nation/getSignQrCode?msg='+msg,
                dataType: 'json',
                type: 'get',
                success: function(data) {
                    console.log(data);
                    if(data.code==0){
                        $('#signTid').val(data.data.tid);
                        $('#signQrCode').attr('src', 'data:image/png;base64,'+data.data.qrCodeImageBase64);
                        $('#signQrCode').show();
                        intervalId = setInterval(function(){
                            querySignScannedStatus(data.data.tid,intervalId)
                        }, 1000)
                    }
                },
                error: function(xhr, status, error) {
                    console.log(error);
                }
            });
        }
        //获取签名二维码扫描状态
        function querySignScannedStatus(tid,intervalId) {
            $.ajax({
                url: '/nation/querySignScannedStatus?tid='+tid,
                dataType: 'json',
                type: 'get',
                success: function(data) {
                    console.log('签名二维码扫描状态')
                    console.log(data);
                    if(data.code==0){
                        if(data.data.scannedStatus=='00'){
                        }else if(data.data.scannedStatus=='01'){
                            clearInterval(intervalId);
                            nIntervalId=setInterval(function (){
                                getSignResult(tid,nIntervalId)
                            },1000)
                        }else{
                            clearInterval(intervalId);
                        }
                    }
                },
                error: function(xhr, status, error) {
                    console.log(error);
                }
            });
        }
        //获取签名结果
        function getSignResult(tid,intervalId) {
            var msg=$('#signMsg').val()
            $.ajax({
                url: '/nation/getSignResult?tid='+tid,
                dataType: 'json',
                type: 'get',
                success: function(data) {
                    console.log('签名结果:')
                    console.log(data);
                    if(data.code==0&&data.data.messageSignatureValue!=null){
                        clearInterval(intervalId)
                        $('#signResult').val(JSON.stringify(data.data))
                        verifySign(encodeURIComponent(msg),encodeURIComponent(data.data.messageSignatureValue),data.data.publicKeyAlgorithm)
                    }
                },
                error: function(xhr, status, error) {
                    console.log(error);
                }
            });
        }
        //验签二维码
        function verifySign(msg,sign,algorithm) {
            $.ajax({
                url: '/nation/verifySign?msg='+msg+'&sign='+sign+'&alogrithm='+algorithm,
                dataType: 'json',
                type: 'get',
                success: function(data) {
                    console.log(data);
                    if(data.code==0&&data.data.verifySignatureResult==true){
                        alert('验签成功')
                    }
                },
                error: function(xhr, status, error) {
                    console.log(error);
                }
            });
        }
    </script>

</head>
<body>
    <h2 style="background-color: rgb(148, 240, 240);padding: 5px 20px;">操作页面</h2>
    <div style="padding: 20px;border: 1px solid #ccc;">
        <div class="layoutFlex">
            <button onclick="generateQRCode()">登录二维码</button>
            <input type="text" id="loginTid" readonly="readonly" placeholder="登录二维码tid"/>
            <img  id="loginQrCode" src="" alt="登录二维码" style="display: none;  ">
            <label id="loginStatus"></label>
            <textarea id="authUser" readonly="readonly"></textarea>
        </div>
        <div class="layoutFlex">
            <button onclick="generateEnQRCode()">加密二维码</button>
            <img id="enQrCode" src="" alt="加密二维码" style="display: none;">
            <input type="text" id="encryptTid" readonly="readonly" placeholder="加密二维码tid">
            <input type="text" id="waitEnMsg" placeholder="待加密字符串" value="12345">
            <textarea id="enMsg" readonly="readonly"></textarea>
        </div>
        <div class="layoutFlex">
            <button onclick="generateDeQRCode()">解密二维码</button>
            <img id="deQrCode" src="" alt="解密二维码" style="display: none;">
            <input type="text" id="decryptTid" readonly="readonly" placeholder="解密二维码tid">
            <textarea id="deMsg"></textarea>
        </div>
        <div class="layoutFlex" >
            <button onclick="generateSignQRCode()">签名二维码</button>
            <img id="signQrCode" src="" alt="签名二维码" style="display: none;">
            <input type="text" id="signMsg" readonly="readonly" placeholder="待签名字符串" value="123456">
            <input type="text" id="signTid" readonly="readonly" placeholder="签名二维码tid">
            <textarea id="signResult"></textarea>
        </div>

        <div class="layoutFlex">
            <button onclick="window.location.href='/signpdf'">pdf签章</button>
            <button onclick="window.location.href='/encrypt'">文件加密</button>
            <button onclick="window.location.href='http://47.108.21.195:8868'">h5签章</button>
        </div>
    </div>
</body>
</html>